﻿using Abp.Application.Services.Dto;
using Abp.Web.Mvc.Authorization;
using Akso.Authorization;
using Akso.Bills;
using Akso.DataMaintains;
using Akso.Schools;
using Akso.Web.Areas.FinanceModule.Models.Bills;
using Akso.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Akso.Web.Areas.FinanceModule.Controllers
{
    public class BillsController : AbpZeroTemplateControllerBase
    {
        // GET: FinanceModule/Bills
        private readonly IBillAppService _billsAppService;
        private readonly ISchoolAppService _schoolsAppService;
        private readonly IDataMaintainAppService _dataMaintainAppService;
        public BillsController(IBillAppService billsAppService, ISchoolAppService schoolsAppService, IDataMaintainAppService dataMaintainAppService)
        {
            _billsAppService = billsAppService;
            _schoolsAppService = schoolsAppService;
            _dataMaintainAppService = dataMaintainAppService;
        }
        public ActionResult Index()
        {
            var model = new BillsViewModel
            {
                FilterText = Request.QueryString["filterText"]
            };
            return View(model);
        }
        /// <summary>
        /// 账单收入编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_FinanceModule_Bills_Create, AppPermissions.Pages_FinanceModule_Bills_Edit)]
        public async Task<PartialViewResult> CreateOrEditIncomeModal(Guid? id)
        {
            var output = await _billsAppService.GetObjectForEdit(new NullableIdDto<Guid> { Id = id });

            var schoolList = _schoolsAppService.GetAllSchools().Items.Select(p => new ComboboxItemDto(p.Id.ToString(), p.SchoolName)).ToList();
            //schoolList.Insert(0, new ComboboxItemDto("", ""));
            var ieTypeList = _dataMaintainAppService.GetListIncomeExpendTypes(1).Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.IETypeName)).ToList();
            //ieTypeList.Insert(0, new ComboboxItemDto("", ""));
            var billAccountList = _dataMaintainAppService.GetListBillAccounts().Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.AccountName)).ToList();
            //billAccountList.Insert(0, new ComboboxItemDto("", ""));
            var checkProjectList= _dataMaintainAppService.GetListOtherChecks(1).Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.CheckName)).ToList();
            checkProjectList.Insert(0, new ComboboxItemDto("", ""));
            var checkCustomerList = _dataMaintainAppService.GetListOtherChecks(2).Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.CheckName)).ToList();
            checkCustomerList.Insert(0, new ComboboxItemDto("", ""));
            var checkStaffList = _dataMaintainAppService.GetListOtherChecks(3).Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.CheckName)).ToList();
            checkStaffList.Insert(0, new ComboboxItemDto("", ""));
            if (id!=null)
            {
                foreach (var item in schoolList)
                {
                    if (item.Value == output.Bill.SchoolId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach(var item in ieTypeList)
                {
                    if (item.Value == output.Bill.BillTypeId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                
                foreach (var item in billAccountList)
                {
                    if (item.Value == output.Bill.BillAccountId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in checkProjectList)
                {
                    if (item.Value == output.Bill.CheckProjectId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in checkCustomerList)
                {
                    if (item.Value == output.Bill.CheckCustomerId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in checkStaffList)
                {
                    if (item.Value == output.Bill.CheckUserId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
            }
            var viewModel = new CreateOrEditIncomeModalViewModel(output);
            viewModel.GetSchoolList = schoolList;
            viewModel.GetIETypeList = ieTypeList;
            viewModel.GetAccountList = billAccountList;
            viewModel.GetCheckProjectList = checkProjectList;
            viewModel.GetCheckCustomerList = checkCustomerList;
            viewModel.GetCheckStaffList = checkStaffList;
            return PartialView("_CreateOrEditIncomeModal", viewModel);
        }
        /// <summary>
        /// 账单支出编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_FinanceModule_Bills_Create, AppPermissions.Pages_FinanceModule_Bills_Edit)]
        public async Task<PartialViewResult> CreateOrEditExpendModal(Guid? id)
        {
            var output = await _billsAppService.GetObjectForEdit(new NullableIdDto<Guid> { Id = id });

            var schoolList = _schoolsAppService.GetAllSchools().Items.Select(p => new ComboboxItemDto(p.Id.ToString(), p.SchoolName)).ToList();
            //schoolList.Insert(0, new ComboboxItemDto("", ""));
            var ieTypeList = _dataMaintainAppService.GetListIncomeExpendTypes(2).Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.IETypeName)).ToList();
            //ieTypeList.Insert(0, new ComboboxItemDto("", ""));
            var billAccountList = _dataMaintainAppService.GetListBillAccounts().Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.AccountName)).ToList();
            //billAccountList.Insert(0, new ComboboxItemDto("", ""));
            var checkProjectList = _dataMaintainAppService.GetListOtherChecks(1).Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.CheckName)).ToList();
            checkProjectList.Insert(0, new ComboboxItemDto("", ""));
            var checkCustomerList = _dataMaintainAppService.GetListOtherChecks(2).Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.CheckName)).ToList();
            checkCustomerList.Insert(0, new ComboboxItemDto("", ""));
            var checkStaffList = _dataMaintainAppService.GetListOtherChecks(3).Items.Select(x => new ComboboxItemDto(x.Id.ToString(), x.CheckName)).ToList();
            checkStaffList.Insert(0, new ComboboxItemDto("", ""));
            if (id != null)
            {
                foreach (var item in schoolList)
                {
                    if (item.Value == output.Bill.SchoolId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in ieTypeList)
                {
                    if (item.Value == output.Bill.BillTypeId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }

                foreach (var item in billAccountList)
                {
                    if (item.Value == output.Bill.BillAccountId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in checkProjectList)
                {
                    if (item.Value == output.Bill.CheckProjectId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in checkCustomerList)
                {
                    if (item.Value == output.Bill.CheckCustomerId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in checkStaffList)
                {
                    if (item.Value == output.Bill.CheckUserId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
            }
            var viewModel = new CreateOrEditIncomeModalViewModel(output);
            viewModel.GetSchoolList = schoolList;
            viewModel.GetIETypeList = ieTypeList;
            viewModel.GetAccountList = billAccountList;
            viewModel.GetCheckProjectList = checkProjectList;
            viewModel.GetCheckCustomerList = checkCustomerList;
            viewModel.GetCheckStaffList = checkStaffList;
            return PartialView("_CreateOrEditExpendModal", viewModel);
        }
    }
}